//
//  BaseTabBarViewController.swift
//  Retail
//
//  Created by zhangjikuan on 2020/6/29.
//  Copyright © 2020 aqdeal. All rights reserved.
//

import Foundation
import UIKit
class BaseTabBarController: UITabBarController {
    override func viewDidLoad() {
        readTabBarConfig()
        
    }
    override func viewDidDisappear(_ animated: Bool) {

    }
    func readTabBarConfig() {
        // 获取plist文件路径
        let path:String = Bundle.main.path(forResource: "TabBarConfig", ofType: "plist") ?? ""
        let result = NSArray(contentsOfFile: path)
        let count:Int = result?.count ?? 0
    
        var list = Array<UIViewController>()
        
        for index in 0...count - 1{
            let itemConfig = result?[index] as Any
            let nav = navigationController(itemConfig as! Dictionary<String, Any>)
            list.append(nav)
        }
        
        print(list)
        self.viewControllers = list
    }
    
    func navigationController(_ config:Dictionary<String, Any>) -> BaseNavigationController {
        let tabBarConfig = TabBarConfig.init()
        tabBarConfig.config(config)
        
        let instanceCtr = InstanceFactory.instance.createInstanceController(tabBarConfig.className!)
        let nav =  BaseNavigationController.init(rootViewController: instanceCtr as! UIViewController)
        
        nav.tabBarItem.title = tabBarConfig.tabBarItem_title!
        nav.tabBarItem.image = UIImage(named: tabBarConfig.normalImageName!)
        nav.tabBarItem.selectedImage = UIImage(named: tabBarConfig.selectImageName!)
        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.brown], for: .normal)
        
        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.red], for: .selected)
  
        return nav
    }
}
